package lampachat_client;

import java.awt.event.WindowEvent;
import java.io.IOException;

/**
 *
 * @author alex
 */
public class JOptions extends javax.swing.JFrame {

    /**
     * Creates new form JLogin
     */
    Options opt = new Options();
    JHome home;

    public JOptions(JHome home) {
        this.home = home;
        initComponents();
        this.setLocationRelativeTo(null);
        this.setEnabled(true);
        this.setVisible(true);
        jButtonCancel.requestFocus();
        jLabelLang.setEnabled(false);
        jComboBoxLang.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jButtonDefault = new javax.swing.JButton();
        jAdress = new javax.swing.JTextField();
        jPort1 = new javax.swing.JTextField();
        jButtonCancel = new javax.swing.JButton();
        jButtonApply = new javax.swing.JButton();
        jLabelServerAdress = new javax.swing.JLabel();
        jLabelServerPort = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelPortWiki1 = new javax.swing.JLabel();
        jCheckBoxSounds = new javax.swing.JCheckBox();
        jComboBoxLang = new javax.swing.JComboBox<>();
        jLabelLang = new javax.swing.JLabel();
        jLabelServerPort1 = new javax.swing.JLabel();
        jPort2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("options");
        setAlwaysOnTop(true);
        setPreferredSize(new java.awt.Dimension(630, 290));
        setResizable(false);
        setSize(new java.awt.Dimension(630, 290));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jButtonDefault.setText("Default all");
        jButtonDefault.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDefaultMouseClicked(evt);
            }
        });

        jAdress.setText("localhostadress");

        jPort1.setText("80800");

        jButtonCancel.setText("Cancel");
        jButtonCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelMouseClicked(evt);
            }
        });

        jButtonApply.setText("Apply");
        jButtonApply.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonApplyMouseClicked(evt);
            }
        });

        jLabelServerAdress.setText("server adress:");

        jLabelServerPort.setText("server port 1:");

        jLabelPortWiki1.setText("on the server should be open the ports you specified");

        jCheckBoxSounds.setText("- sound notifications");

        jComboBoxLang.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "English", "Russian" }));

        jLabelLang.setText("Language:");

        jLabelServerPort1.setText("server port 2:");

        jPort2.setText("80800");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabelServerAdress)
                            .addComponent(jLabelServerPort)
                            .addComponent(jLabelServerPort1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jAdress)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPort2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jPort1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabelPortWiki1)))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jComboBoxLang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                        .addComponent(jButtonDefault)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonApply))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelLang)
                            .addComponent(jCheckBoxSounds))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAdress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelServerAdress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPort1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelServerPort)
                    .addComponent(jLabelPortWiki1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPort2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelServerPort1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxSounds)
                .addGap(72, 72, 72)
                .addComponent(jLabelLang)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonApply)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonDefault)
                    .addComponent(jComboBoxLang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        jAdress.setText(Options.getServer_adress());
        jPort1.setText(Integer.toString(Options.getPort1()));
        jPort2.setText(Integer.toString(Options.getPort2()));
        jCheckBoxSounds.setSelected(Options.isSound_notifications());
        jComboBoxLang.setToolTipText(Options.getLang());
    }//GEN-LAST:event_formWindowOpened
    private void jButtonCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelMouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonCancelMouseClicked
    private void jButtonApplyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonApplyMouseClicked
        // TODO add your handling code here:
        try {
            opt.setOptionsInFile(jAdress.getText(),
                    Integer.parseInt(jPort1.getText()),
                    Integer.parseInt(jPort2.getText()),
                    jCheckBoxSounds.isSelected(),
                    jComboBoxLang.getItemAt(jComboBoxLang.getSelectedIndex()));
        } catch (IOException ex) {
            System.out.println(ex);
        }
        home.dispatchEvent(new WindowEvent(home, WindowEvent.WINDOW_CLOSING));

    }//GEN-LAST:event_jButtonApplyMouseClicked

    private void jButtonDefaultMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDefaultMouseClicked
        // TODO add your handling code here:
        jPort1.setText(Integer.toString(opt.getDefault_port1()));
        jPort2.setText(Integer.toString(opt.getDefault_port2()));
        jAdress.setText(opt.getDefault_server_adress());
        jCheckBoxSounds.setSelected(opt.isDefault_sound_notifications());
        jComboBoxLang.setSelectedIndex(0);
    }//GEN-LAST:event_jButtonDefaultMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JOptions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JOptions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JOptions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JOptions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        //   java.awt.EventQueue.invokeLater(new Runnable() {
        //     public void run() {
        //        new JOptions().setVisible(true);
        //    }
        //});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAdress;
    private javax.swing.JButton jButtonApply;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDefault;
    private javax.swing.JCheckBox jCheckBoxSounds;
    private javax.swing.JComboBox<String> jComboBoxLang;
    private javax.swing.JLabel jLabelLang;
    private javax.swing.JLabel jLabelPortWiki1;
    private javax.swing.JLabel jLabelServerAdress;
    private javax.swing.JLabel jLabelServerPort;
    private javax.swing.JLabel jLabelServerPort1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jPort1;
    private javax.swing.JTextField jPort2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
